using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FORUs
{
    partial class UltrastarSong
    {
        public static UltrastarSong Empty
        {
            get
            {
                UltrastarSong usEmpty = new UltrastarSong();
                usEmpty._sFilename = string.Empty;
                return usEmpty;
            }
        }
        
        /// <summary>
        /// Load an Ultrastar Song from a File
        /// </summary>
        /// <param name="filename">File to load (absolute name)</param>
        /// <returns>The UltrastarSong object</returns>
        public static UltrastarSong FromFile(string filename)
        {
            UltrastarSong usSong = new UltrastarSong();
            StreamReader reader = null;
            string sLine;

            // If the file doesn't exist
            if (!File.Exists(filename))
                return Empty;

            // Loading the text file
            try
            {
                usSong._sFilename = filename;
                reader = new StreamReader(filename, Encoding.Default, true);
                sLine = reader.ReadLine();
                while (sLine != null)
                {
                    // Parsing every line of the file
                    if (sLine.StartsWith("#"))
                    {
                        usSong.ParseGetAccessor(sLine.Substring(1, sLine.IndexOf(":") - 1),
                            sLine.Substring(sLine.IndexOf(":") + 1));
                    }

                    sLine = reader.ReadLine();
                }
            }
            catch (Exception)
            {
                usSong = Empty;
            }
            finally
            {
                reader.Close();
            }

            // Checking the informations which have been parsed
            if (usSong.Mp3Filename.Equals(string.Empty) || usSong.Artist.Equals(string.Empty)
                    || usSong.Title.Equals(string.Empty))
                return Empty;
            else
                return usSong;

        }

        private void ParseGetAccessor(string fileAccessor, string value)
        {
            switch (fileAccessor)
            {
                case "TITLE":
                    _sTitle = value;
                    break;
                case "ARTIST":
                    _sArtist = value;
                    break;
                case "MP3":
                    _sMp3Filename = value;
                    break;
                case "COVER":
                    _sCoverFilename = value;
                    break;
                case "BACKGROUND":
                    _sBackgroundFilename = value;
                    break;
                case "GENRE":
                    _sGenre = value;
                    break;
                case "EDITION":
                    _sEdition = value;
                    break;
                case "LANGUAGE":
                    _sLanguage = value;
                    break;
            }
        }

        #region Comparison Delegates Functions
        public static int CompareByLanguage(UltrastarSong uSong1, UltrastarSong uSong2)
        {
            return uSong1._sLanguage.CompareTo(uSong2._sLanguage);
        }

        public static int CompareByArtist(UltrastarSong uSong1, UltrastarSong uSong2)
        {
            return uSong1._sArtist.CompareTo(uSong2._sArtist);
        }

        public static int CompareByTitle(UltrastarSong uSong1, UltrastarSong uSong2)
        {
            return uSong1._sTitle.CompareTo(uSong2._sTitle);
        }

        public static int CompareByEdition(UltrastarSong uSong1, UltrastarSong uSong2)
        {
            return uSong1._sEdition.CompareTo(uSong2._sEdition);
        }

        public static int CompareByFilename(UltrastarSong uSong1, UltrastarSong uSong2)
        {
            return uSong1._sFilename.CompareTo(uSong2._sFilename);
        }

        public static int CompareByGenre(UltrastarSong uSong1, UltrastarSong uSong2)
        {
            return uSong1._sGenre.CompareTo(uSong2._sGenre);
        }

        public static int CompareByCompleteTitle(UltrastarSong uSong1, UltrastarSong uSong2)
        {
            return uSong1.CompleteTitle.CompareTo(uSong2.CompleteTitle);
        }
        #endregion
    }
}
